import Cookies from 'js-cookie'
import _tools from '@/utils/_tools';

const TokenKey = 'token'
const UserLogin = 'user_login'
const UserInfoKey = 'userInfo';
export function getToken() {
  return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
  localStorage.removeItem(UserLogin)
  return localStorage.removeItem(TokenKey)
}

export function setUserInfo(userInfo) {
  userInfo = JSON.stringify(userInfo);
  userInfo = _tools.encrypt(userInfo);
  return localStorage.setItem(UserInfoKey, userInfo)
}

export function getUserInfo() {
  let userInfo = localStorage.getItem(UserInfoKey)
  if (userInfo) {
    userInfo = JSON.parse(_tools.decode_q(userInfo))
  }
  return userInfo
}

export function loginState() {
  let userInfo = getUserInfo()
  if (!userInfo) return false;
  let newTime = Date.parse(new Date()) / 1000;
  // 一天
  if (userInfo.loginTime + 86400 > newTime) {
    return true;
  } else {
    return false;
  }
}